package com.zt.rongda.stock.service;

import java.text.SimpleDateFormat;
import com.zt.rongda.stock.inter.StockTypeMapper;
import com.zt.rongda.stock.model.StockType;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseTreeService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StockTypeService extends BaseTreeService {

    @Autowired
    private StockTypeMapper stockTypeMapper;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select * from STOCK_TYPE where 1=1 ");

        sqlBuilder.addWhereLike2("EMP_NAME", reqBean.getInfo("empName"));
        sqlBuilder.addWhereLike2("C_STATUS", reqBean.getInfo("status"));

        // 时间间隔
        String endDate = reqBean.getInfo("makeTimeEnd");
        if(endDate != null && endDate.length()>=10)
        {
            Date tmp_date =  DateUtil.parse(endDate);

            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            endDate = df.format(new Date(tmp_date.getTime() + 1 * 24 * 60 * 60 * 1000)); // 结束日期加一天
        }
        sqlBuilder.addTimeWhere("MAKE_TIME", reqBean.getInfo("makeTimeStart"), ">=");
        sqlBuilder.addTimeWhere("MAKE_TIME", endDate, "<");
        //

        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 获取树形菜单
     *
     * @return
     */
    public JSONArray queryTree() {

        String sql = "select TYPE_ID typeId, TYPE_NAME typeName, PARENT_TYPE_ID parentTypeId, UNIT unit, ALERT_NUM alertNum from STOCK_TYPE";
        return queryTreeGridFlat("typeId", "parentTypeId", query(sql));
    }

    /**
    * 通过id查询
    *
    * @param typeId
    * @return
    */
    public StockType getById(String typeId) {
        return stockTypeMapper.selectByPrimaryKey(typeId);
    }

    /**
    * 新增
    *
    * @param stockType
    */
    public void add(StockType stockType) {
        stockType.setTypeId(stringId());// 手动设置Id

        stockTypeMapper.insert(stockType);
    }

    /**
    * 修改
    *
    * @param stockType
    */
    public void update(StockType stockType) {
        String typeId = stockType.getTypeId();
        if (StringUtil.isEmpty(typeId)) {
            throw new BusinessException("获取主键失败");
        }

        stockTypeMapper.updateByPrimaryKeySelective(stockType);
    }

    /**
    * 删除
    *
    * @param typeId
    */
    public void delete(String typeId) {
        if (StringUtil.isEmpty(typeId)) {
            throw new BusinessException("获取主键失败");
        }
        stockTypeMapper.deleteByPrimaryKey(typeId);
    }

    /**
     * 分类材料树
     * @return
     */
    public JSONArray stockTree() {
        String sql = "SELECT CONCAT('T','_',t.TYPE_ID) id, t.TYPE_NAME name,CONCAT('T','_',t.PARENT_TYPE_ID) parentId," +
                "t.UNIT unit,'spec' spec FROM STOCK_TYPE t " +
                "UNION ALL " +
                "SELECT s.STOCK_ID id, s.STOCK_NAME NAME, concat('T','_',s.TYPE_ID) parentId," +
                "s.UNIT unit,s.SPEC spec FROM STOCK s";
        return query(sql);
    }
}